package com.heritage.repository;

import com.heritage.entity.Heritage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface HeritageRepository extends JpaRepository<Heritage, Long> {
    List<Heritage> findByCategoryId(Long categoryId);

    @Query("SELECT h FROM Heritage h LEFT JOIN FETCH h.category ORDER BY h.createdAt DESC")
    List<Heritage> findAllWithCategory();

    @Query("SELECT h FROM Heritage h LEFT JOIN FETCH h.comments WHERE h.id = :id")
    Optional<Heritage> findByIdWithComments(@Param("id") Long id);

    @Query("SELECT h FROM Heritage h LEFT JOIN FETCH h.likes WHERE h.id = :id")
    Optional<Heritage> findByIdWithLikes(@Param("id") Long id);

    @Query("SELECT h FROM Heritage h LEFT JOIN FETCH h.category WHERE h.id = :id")
    Optional<Heritage> findByIdWithCategory(@Param("id") Long id);

}
